const request = require('../../utils/https.js')
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    userId = wx.getStorageSync('userId');
    console.log('编辑身份==>', options)
    //oneAdd有值为新增 空值为编辑
    var oneAdd = options.add



    wx.login({
      success: (suc) => {
        if (suc.code) {
          wx.request({
            url: getApp().Api_url + 'user/getSk',
            data: {
              userId: userId,
              jsCode: suc.code,
            },
            success(succ) {
              if (succ.data.code == 200) {
                wx.setStorageSync("sKey", succ.data.data);

                if (oneAdd == 'oneAdd') {
                  console.log('新增===>')
                  that.setData({
                    state: 0 
                  })

                } else {
                  console.log('编辑===>')
                  that.setData({
                    state: 1
                  })
                  request._get(getApp().Api_url + 'user/hasCert', {
                    userId: userId
                  }, function (res) {
                    if (res.data.errCode == 200) {
                      console.log('显示信息==>', res.data)
                      that.setData({
                        username: res.data.data.username,
                        identityCode: res.data.data.idCard,
                        bankCard: res.data.data.bankCard,
                        phone: res.data.data.phone,
                        imgPositive: res.data.data.idCardFront,
                        imgReverse: res.data.data.idCardBack
                      })

                    }
                  }, function () {})

                }


              } else {
                wx.showToast({
                  title: '登录状态异常,请稍后重试',
                  icon: 'none'
                })
              }
            }
          })
        }
      }
    })



  },
  //授权手机号
  getPhoneNumber(e) {
    const that = this;
    var skeys = wx.getStorageSync("sKey");
    console.log('获取手机号====>>', skeys)
    util._getPhoneNumber(e, that, skeys)
  },
  //获取姓名
  getUserName(e) {
    const that = this;
    let userName = e.detail.detail.value;
    that.setData({
      username: userName
    })
  },
  //获取身份证号
  getPhone(e) {
    const that = this;
    let identityCode = e.detail.detail.value;
    that.setData({
      identityCode: identityCode
    })
  },
  //银行卡号
  getBankcard(e) {
    const that = this;
    let bankCard = e.detail.detail.value;
    that.setData({
      bankCard: bankCard
    })
  },
  //开户行
  getBank(e) {
    const that = this;
    let bank = e.detail.detail.value;
    that.setData({
      bank: bank
    })
  },
  // 验证身份证号码 
  isPoneAvailable(str) {
    var myreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!myreg.test(str)) {
      return false;
    } else {
      return true;
    }
  },
  //校验银行卡号
  isCheckBank(str) {
    var myreg = /[1-9]\d{12,18}/;
    if (!myreg.test(str)) {
      return false;
    } else {
      return true;
    }
  },
  /**
   * 身份证背面照  上传
   */
  choosePic2(e) {
    let that = this;
    //图片选择
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths===>', tempFilePaths)
        that.setData({
          imgReverse_img: tempFilePaths[0]
        })
        // 图片上传
        wx.uploadFile({
          url: getApp().Api_url + 'rank/upImg',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success(response) {

            let resultD = JSON.parse(response.data);

            console.log('11111111===>', resultD)

            if (resultD.code == 200) {
              console.log('resultD==>', resultD)
              that.setData({
                imgReverse: resultD.data
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },
  dePic2() {
    const that = this;
    that.setData({
      imgReverse: ''
    })
  },

  /*
    身份证正面照
  */
  choosePic1(e) {
    let that = this;
    //图片选择
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths===>', tempFilePaths)

        that.setData({
          imgPositive_img: tempFilePaths[0]
        })

        // 图片上传
        wx.uploadFile({
          url: getApp().Api_url + 'rank/upImg',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success(response) {
            let resultD = JSON.parse(response.data);
            console.log('11111111===>', resultD)
            if (resultD.code == 200) {
              console.log('resultD==>', resultD)
              that.setData({
                imgPositive: resultD.data
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },
  dePic1() {
    const that = this;
    that.setData({
      imgPositive: ''
    })
  },
  postSubmit() {
    const that = this;
    var username = that.data.username;
    var identityCode = that.data.identityCode;
    var bankCard = that.data.bankCard;
    var idCardBack = that.data.imgReverse;
    var idCardFront = that.data.imgPositive;
    var phone =wx.getStorageSync('phone')?wx.getStorageSync('phone'):that.data.phone;
    console.log('idCardBack==>', idCardBack)
    console.log('idCardFront==>', idCardFront)
    console.log('phone==>', phone)
    console.log('username==>', username)
    console.log('identityCode==>', identityCode)
    console.log('bankCard==>', bankCard)

    //判断信息填写完整没有
    if (that.isPoneAvailable(identityCode)) {
      if (that.isCheckBank(bankCard)) {
        if (username) {

          if(!idCardFront || idCardFront== 'undefined' || idCardFront==undefined ){//正面有
            wx.showToast({
              title: '请上传身份证正面照',
              icon: 'none',
              duration: 2000
            })
            return false;
          }
          
          if(!idCardBack || idCardBack== 'undefined' || idCardBack==undefined ){//背面有
            wx.showToast({
              title: '请上传身份证背面照',
              icon: 'none',
              duration: 2000
            })
            return false;
          }
          
          request._post(getApp().Api_url + 'user/doCert', { //认证
            userId: userId,
            username: username,
            bankCard: bankCard,
            idCard: identityCode,
            idCardBack: idCardBack,
            idCardFront: idCardFront,
            phone: phone
          }, function (res) {

            console.log('提交身份证号码===>', res.data.errCode)

            if (res.data.errCode == 200) {
              wx.redirectTo({
                url: '/pages/withdrawal/withdrawal'
              })
            } else {
              wx.showToast({
                title: '提交失败',
                icon: 'none',
                duration: 2000
              })
            }
          }, function () {})
        } else {
          wx.showToast({
            title: '信息不完整！',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: '银行卡号有误，请重新输入！',
          icon: 'none',
          duration: 2000
        })
      }

    } else {
      wx.showToast({
        title: '身份证号码有误，请重新输入！',
        icon: 'none',
        duration: 2000
      })

    }
  },
})